package com.wechat.Exception;

import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

public class CustomExceptionResolver implements HandlerExceptionResolver {

	@Override
	/**
	 * 全局的异常处理器
	 */
	public ModelAndView resolveException(HttpServletRequest arg0, 
			HttpServletResponse arg1, Object handle,Exception ex) {
		Logger log=LogManager.getLogger("wechat");
		String className = handle.toString();
		log.error(className+" : "+getStackTrace(ex));
		return null;
	}
	/**
	 * 获取完整异常信息
	 *
	 *@dagewang 2017年10月30日
	 */
	public static String getStackTrace(Throwable t)
	{
	StringWriter sw = new StringWriter();
	PrintWriter pw = new PrintWriter(sw);


	try
	{
	t.printStackTrace(pw);
	return sw.toString();
	}
	finally
	{
	pw.close();
	}
	}
}
